//
//  LoginViewController.m
//  NeusoftHBSJSober
//
//  Created by Apple－12 on 15/10/28.
//  Copyright (c) 2015年 com.neusoftsober. All rights reserved.
//

#import "LoginViewController.h"
#import "MHomeViewController.h"
@interface LoginViewController ()

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

//    self.loginButton.enabled=NO;
    usernameText.delegate = self;
    passwordText.delegate = self;
    
    if (switchTouch.isOn) {
        [passwordText setSecureTextEntry:NO];

    }else{
        [passwordText setSecureTextEntry:YES];
    }
    switchImage.image = [UIImage imageNamed:@"ic_password_hidden.png"];
    [self.view sizeToFit];
}
-(void)viewWillAppear:(BOOL)animated{
    usernameText.text = nil;
    passwordText.text = nil;
}

-(IBAction)tapSwitch:(id)sender{
    
    if (switchTouch.isOn) {
        
        [passwordText setSecureTextEntry:NO];
        switchImage.image = [UIImage imageNamed:@"ic_password_visible.png"];
    }else{
        
        [passwordText setSecureTextEntry:YES];
        
        switchImage.image = [UIImage imageNamed:@"ic_password_hidden.png"];
    }
    
}
/*
-(IBAction)loginButton:(id)sender{

    [[MRegistService sharedClient] getLoginWithUsername:usernameText.text password:passwordText.text completion:^(MUserModel *resultLogin) {
        NSLog(@"------------账号%@",resultLogin.username);
        NSLog(@"------------密码%@",resultLogin.password);
        
        if ([resultLogin.username isEqualToString:usernameText.text]) {
            
            
            UIAlertView *alert;
            alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"密码正确" delegate:self cancelButtonTitle:@"登陆" otherButtonTitles:nil, nil];
            [alert show];
//            usernameText.text = NULL;
//            passwordText.text = NULL;
//            [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(show) userInfo:nil repeats:NO];

        }
        
        
        
    } failure:^(NSString *error) {
        
        UIAlertView *alert;
        if (![usernameText.text isEqualToString:[userDefaults objectForKey:@"username"]]) {
           
            
            alert = [[UIAlertView alloc]initWithTitle:@"警告" message:@"账户不存在" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }else if (![passwordText.text isEqualToString:[userDefaults objectForKey:@"password"]]){
            alert = [[UIAlertView alloc]initWithTitle:@"警告" message:@"密码错误" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }

        usernameText.text = NULL;
        passwordText.text = NULL;
        
    }];
    [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(show) userInfo:nil repeats:NO];
    activityIndicatorView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    activityIndicatorView .center = self.view.center;
    
    [self.view addSubview:activityIndicatorView];
    
    activityIndicatorView.color = [UIColor blueColor];
    [activityIndicatorView startAnimating];
    
    [activityIndicatorView setHidesWhenStopped:YES];

    

}
 */
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (usernameText.tag == 100) {
        if (![usernameText.text isEqualToString:@""]&&![passwordText.text isEqualToString:@""]) {
            self.loginButton.enabled = YES;
            [self.loginButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
            
            
        }
        
        
    }
    return YES;
}

-(void)show{
    

    [activityIndicatorView stopAnimating];
    NSLog(@"进入首页");
    MHomeViewController *homePage = [[MHomeViewController alloc]init];
    [self presentViewController:homePage animated:YES completion:nil];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(IBAction)ForgetIDButton:(id)sender{
    registViewController = [[RegistViewController alloc]init];
    [self presentViewController:registViewController animated:YES completion:nil];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if(buttonIndex == 1){
        
        
        
        NSString *username = usernameText.text;
        NSString *password = passwordText.text;
        
        [userDefaults1 setObject:username forKey:@"username1"];
        [userDefaults1 setObject:password forKey:@"password1"];
        
        
        NSLog(@"%@",[userDefaults1 objectForKey:@"username"]);
        
        
        
//        SetSecretViewController *setsecret = [[SetSecretViewController alloc]init];
//        [self presentViewController:setsecret animated:YES completion:nil];
//        NSLog(@"跳转设置4位密码界面");
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(IBAction)tapBackground:(id)sender{
    [usernameText resignFirstResponder];
    [passwordText resignFirstResponder];
}

@end
